(function(){
    "use strict";
    var onlineTestProcessController=function ($scope,$http,$modal,online_testService,objectIdService){
        //初始化全局变量的项目ID号
        $scope.projectId = '';

        $scope.productionId = '';

        //初始化项目下所有的产品的所有涉及人员信息
        $scope.operators = [];
        //初始化项目下所有的产品信息
        $scope.pros = [];

        //获取所有全局变量的值
        $scope.rootParasGet1 = function (){
            $scope.projectId = objectIdService('0');
            $scope.pros = prosService([],true);
            if ($scope.productionId == ''){
                $scope.productionId = $scope.pros[0].productKey;
            }
            $scope.operators = opersService([],true);
            // $scope.load1();
        };

        //改变选中的产品
        $scope.proChange = function (){
            console.log("当前选择的产品ID:"+$scope.selectedPro.id+$scope.selectedPro.productName);
            $scope.productionId = $scope.selectedPro.productKey;
            $scope.onlineTestProcessGet($scope.projectId,
                                    $scope.productionId,
                                    $scope.selectedTimedimension);
        };

        $scope.timedimensions = ["DAY","WEEK","MONTH"];
        //日期下拉框的change事件方法
        $scope.timedimensionChange = function (){
            var testProcessData = $scope.onlineTestProcessGet($scope.projectId,
                                $scope.productionId,
                                $scope.selectedTimedimension);
            $scope.clearTable();
            $scope.initTable(testProcessData);
        };
        //获取测试进展信息
        $scope.onlineTestProcessGet = function (projectId,productionId,timedimensions){
            var requestJson = {
                projectId : projectId,
                productionId : productionId,
                timedimensions : timedimensions
            };
            online_testService.testProcessGet(requestJson).success(function(data){
                if(data.result.length>0){
                    var testProgressList = data.result;
                    $scope.clearTable();
                    $scope.initTable(testProgressList);
                }
            });
        };

        //柱形图初始方法
        // $scope.load1 = function(){
        //    var testProcessData = $scope.onlineTestProcessGet($scope.projectId,
        //                         $scope.productionId,
        //                         $scope.selectedTimedimension);
        // };

        $scope.chart = new h5Charts.SerialChart();
        $scope.chart1 = new h5Charts.SerialChart();
        $scope.chart2 = new h5Charts.SerialChart();
        //画柱形图表
        $scope.initTable = function(testProcessData){
            if(typeof(testProcessData)== "undefined"){
                return;
            }

            var dataProvider = [];
            var dataProvider1 = [];
            var dataProvider2 = [];
            var colorList = [];
            angular.forEach(testProcessData,function (item) {
                var obj = {};
                obj.name = item.testerName ;
                obj.num = item.testRailCaseNum ;
                dataProvider.push(obj);
                colorList.push("#00B47A");

                var obj1 = {};
                obj1.name = item.testerName ;
                obj1.num = item.jiraNewBugNum ;
                dataProvider1.push(obj1);

                var obj2 = {};
                obj2.name = item.testerName ;
                obj2.num = item.jiraValidBugNum ;
                dataProvider2.push(obj2);
            });

            $scope.chart.dataProvider = dataProvider;
            $scope.chart.categoryField = "name";
            $scope.chart.valueField = "num";
            $scope.chart.type = "column";
            $scope.chart.columnWidth = 30;
            $scope.chart.colors = colorList;
            $scope.chart.addTo("donetestcases");

            $scope.chart1.dataProvider = dataProvider1;
            $scope.chart1.categoryField = "name";
            $scope.chart1.valueField = "num";
            $scope.chart1.type = "column";
            $scope.chart1.columnWidth = 30;
            $scope.chart1.colors = colorList;
            $scope.chart1.addTo("addbugs");

            $scope.chart2.dataProvider = dataProvider2;
            $scope.chart2.categoryField = "name";
            $scope.chart2.valueField = "num";
            $scope.chart2.type = "column";
            $scope.chart2.columnWidth = 30;
            $scope.chart2.colors = colorList;
            $scope.chart2.addTo("validbugs");
        };

        //清空画布内容
        $scope.clearTable = function (){
            var canvas1=document.getElementById("donetestcases");
            var cxt1=canvas1.getContext("2d");
            cxt1.clearRect(0,0,canvas1.width,canvas1.height);

            var canvas2=document.getElementById("addbugs");
            var cxt2=canvas2.getContext("2d");
            cxt2.clearRect(0,0,canvas2.width,canvas2.height);

            var canvas3=document.getElementById("validbugs");
            var cxt3=canvas3.getContext("2d");
            cxt3.clearRect(0,0,canvas3.width,canvas3.height);
        };

        //第二个tab页面(版本测试)部分

        //获取jenkins里的最新构建信息记录
        $scope.buildRecords = [];
        $scope.buildRecordsGet = function (){
            var para = {
                projectId : $scope.projectId
            };
            online_testService.latestBuildInfoGet(para).success(function(data){
                if(data.result == true){
                    angular.forEach(data.latestBuildInfoList,function (item) {
                        var temVar = {
                            productionName : item.productionName,
                            buildDate : item.buildDate,
                            buildTime : item.buildTime,
                            isCheckout : $scope.transferResult(item.isCheckout),
                            isBuild : $scope.transferResult(item.isBuild),
                            highBugNum : item.highBugNum,
                            normalBugNum : item.normalBugNum,
                            coverageReport : item.coverageReport,
                            isGenerateTarFile : $scope.transferResult(item.isGenerateTarFile),
                            isUploadToTestLib : $scope.transferResult(item.isUploadToTestLib),
                            isDeployTestEnvironment : $scope.transferResult(item.isDeployTestEnvironment),
                            isUploadToStableLib : $scope.transferResult(item.isUploadToStableLib)
                        };
                        $scope.buildRecords.push(temVar);
                    });
                }else{
                    console.log('Get codeBuild information error !');
                }
            });
        };
        //将boolean变量值转化成白字
        $scope.transferResult = function (var1){
            if(var1){
                return "成功" ;
            }else {
                return "失败" ;
            }
        };

        //获取最新提测信息记录
        $scope.testStartRecords = [];
        $scope.testStartRecordsGet = function (){
            var para = {
                projectId : $scope.projectId
            };
            online_testService.latestTestStartInfoGet(para).success(function(data){
                if(true){
                    $scope.testStartRecords = data.result;
                }else{
                     console.log('Get codeBuild information error !');
                }
            });
        };

        //第二个tab页面（版本测试）页面的初始化方法
        $scope.rootParasGet2 = function(){
            $scope.buildRecordsGet();
            $scope.testStartRecordsGet();
        };

        //刷新显示版本页面
        $scope.refreshTestStartRecords = function(){
            $scope.testStartRecordsGet();
            console.log('刷新成功 !');
        };

        //删除选中的提测信息记录
        $scope.deleteInfo = function(index){
            var selectedTestStartRecord = $scope.testStartRecords[index];
            $scope.testStartRecords.splice(index,1);
            var paras = {
                deleteId : selectedTestStartRecord.id
            };
            online_testService.testStartInfoDel(paras).success(function(data){
                if(data.result == true){
                    console.log('delete successfully !');
                }else{
                    console.log('Get codeBuild information error !');
                }
            });
        };

        //构建选中的提测信息记录
        $scope.buildOnJenkins = function(index){
            var selectedTestStartRecord = $scope.testStartRecords[index];
            var paras = {
                productionName : selectedTestStartRecord.productionName,
                versionNum : selectedTestStartRecord.versionNum,
                branchName : selectedTestStartRecord.branchName
            };
            online_testService.buildOnJenkins(paras).success(function(data){
                if(data.result == true){
                    console.log('build successfully !');
                }else{
                    console.log('build failed !');
                }
            });
        };

        //对选中的提测信息记录进行部署包归档
        $scope.uploadTarFile = function(index){
            var selectedTestStartRecord = $scope.testStartRecords[index];
            var para = {
                productionName : selectedTestStartRecord.productionName,
                versionNum : selectedTestStartRecord.versionNum
            };
            online_testService.uploadTarFile(para).success(function(data){
                if(data.result == true){
                    console.log('uploadTarFile successfully !');
                }else{
                    console.log('uploadTarFile failed !');
                }
            });
        };

        //打开新增对话框
        $scope.addTestStartRecord = function(){
            var modalInstance = $modal.open({
                templateUrl : 'myModalContent.html',
                controller : modalInstanceCtrl,
                keyboard : false,
                backdrop : false
            });
            modalInstance.result.then(function(data) {
                //在后台数据库保存新增记录
                var para = {
                    projectId : $scope.projectId,
                    iterationTestInfo : data
                };
                online_testService.newTestStartInfoAdd(para).success(function(data){
                    if(true){
                        //保存成功的情况下刷新表格
                        $scope.refreshTestStartRecords();
                    }else{
                        console.log('New Record save failed !');
                    }
                });
            });
        };


    };

    var modalInstanceCtrl = ["$scope","$modal", "$modalInstance",function($scope, $modal, $modalInstance) {

        //日期控件
        $scope.dateOptions = {
             startingDay: 1,
             class: 'datepicker'
        };
        //日期控件
        $scope.format = "yyyy-MM-dd";
        $scope.openDatepicker = function($event, i) {
            $event.preventDefault();
            $event.stopPropagation();
            if(i==1){
                $scope.loanDate = true;
            }
        };

        $scope.openDatepicker1 = function($event, i) {
            $event.preventDefault();
            $event.stopPropagation();
            if(i==1){
                $scope.loanDate1 = true;
            }
        };

        $scope.newTestStartRecord = {
            productionName :  "",
            versionNum :  "",
            testStarterName :  "",
            loanDate : "",
            branchName : "",
            testerName : "",
            loanDate1 :  "",
            smokeTestPassRate : "",
            returnNum :  "",
            configInfo :  ""
        };

        $scope.ok = function() {
            $modalInstance.close($scope.newTestStartRecord);
        };
        $scope.cancel = function() {
            $modalInstance.dismiss('cancel');
        };
    }];

    angular.module("cicd.testprocess",[])
        .controller("OnlineTestProcessController",["$scope","$http","$modal",
            "online_testService","objectIdService","prosService","opersService",onlineTestProcessController])

})();